
from fdfs_client.client import Fdfs_client
from rest_framework import serializers
from django.conf import settings

def custom_upload_by_buffer(content=None, config_path=settings.FDFS_CONFIG_PATH):
    """
    # 自定义fdfs文件上传函数
    :param config_path   fdfs配置文件路径
    :param content   文件内容(字节对象)
    :return: fdfs文件唯一标示
    """
    # 非核心代码，提高程序的健壮性判断
    assert config_path and content, "您必须传入配置文件和文件数据！"

    conn = Fdfs_client(config_path)

    res = conn.upload_by_buffer(content)
    if not (res and res['Status'] == 'Upload successed.'):
        # 上传失败，数据处理失败！
        return serializers.ValidationError("fdfs文件上传失败！")

    # 提取文件id
    file_id = res['Remote file_id']

    return file_id